package com.xuanyuan.material

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.xuanyuan.base.BaseActivity
import com.xuanyuan.material.databinding.ActivityBottomNavigationBinding
import com.xuanyuan.material.databinding.ActivityTabLayoutBinding
import com.xuanyuan.navaction.Fragment2
import com.xuanyuan.navaction.Fragment3
import com.xuanyuan.navaction.Fragment4


class TabLayoutActivity : BaseActivity<ActivityTabLayoutBinding>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 屏幕填充模式
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        // 屏幕滚动模式
//        mBinding.tablayout.tabMode = TabLayout.MODE_SCROLLABLE


        mBinding.tablayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let {
                    // Get badge from tab (or create one if none exists)
                    val badgeDrawable = it.orCreateBadge
                    //val badge = tabLayout.getTabAt(0).orCreateBadge
                    badgeDrawable.isVisible = true
                    // Customize badge
                    badgeDrawable.number = 15
                }
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                // Handle tab reselect
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.let {
                    val badgeDrawable = tab.badge
                    badgeDrawable?.isVisible = false
                    badgeDrawable?.clearNumber()
                }
            }
        })

        initView()

    }


    fun initView() {
        val fragments: MutableList<Fragment> = ArrayList()
        fragments.add(Fragment2())
        fragments.add(Fragment3())
        fragments.add(Fragment4())
        mBinding.viewPage2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(position: Int): Fragment {
                return fragments[position]
            }
        }

        mBinding.viewPage2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }
        })


        TabLayoutMediator(mBinding.tablayout, mBinding.viewPage2) { tab, position ->
            when (position) {
                0 -> {
                    tab.text = "222222222"
                    tab.icon = resources.getDrawable(R.drawable.ic_icon1)
                }
                1 -> {
                    tab.text = "33333333"
                    tab.icon = resources.getDrawable(R.drawable.ic_icon2)
                }

                2 -> {
                    tab.text = "444444444"
                    tab.icon = resources.getDrawable(R.drawable.ic_icon3)

                }
            }
        }.attach()
    }

    override fun initLayout(): Int {
        return R.layout.activity_tab_layout
    }

    override fun afterInit() {

    }


}

